using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using Yoozoo.UI;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

public class RawImageBinder : BinderBase
{
    public enum AttributeType : int
    {
        textureName,
        uvRect,
        onCullStateChanged,
        maskable,
        isMaskingGraphic,
        color,
        raycastTarget,
        enabled,
    }

    private RawImage _target;
    private ResLoader _resLoader;

    public RawImageBinder(UIExpansion owner, LinkerConfig config) : base(owner, config)
    {
    }

    public RawImageBinder()
    {
        
    }

    public override void Init(LinkerConfig config)
    {
        GameObject targetGo = _owner.GetStoredGameObject(config.StoredGameObjectIndex);
        if (targetGo == null)
        {
            return;
        }

        _target = targetGo.GetComponent<RawImage>();
        _active = _target != null;
        _resLoader = ResLoader.Alloc();
    }

    public override void SetString(string value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.textureName:

                if (string.IsNullOrEmpty(value))
                {
                    _target.texture = null;
                    _target.gameObject.SetActive(false);
                    return;
                }
                _resLoader.Add2Load("Assets/ResourcesAssets/" + value + ".png", (success, name, asset) =>
                {
                    if (!success)
                    {
                        UnityEngine.Debug.LogError("dont has this texture:" + value);
                    }
                    else
                    {
                        _target.texture = asset as Texture;
                        _target.gameObject.SetActive(true);
                    }
                }).Load();

                // ResourcesManager.ReadAsync(_target, "Assets/ResourcesAssets/" + value+".png", (obj, r) =>
                // {
                //     if (obj == null)
                //     {
                //         UnityEngine.Debug.LogError("dont has this texture:" + value);
                //     }
                //     else
                //     {
                //         _target.texture = obj as Texture;
                //         _target.gameObject.SetActive(true);
                //     }
                // }, false);
                break;
            default:
                break;
        }

        base.SetString(value);
    }

    public override void SetRect(Rect value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.uvRect:
                _target.uvRect = value;
                break;
            default:
                break;
        }

        base.SetRect(value);
    }

    public override void SetActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.AddListener(action);
                break;
            default:
                break;
        }

        base.SetActionBoolean(action);
    }

    public override void RemoveActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveListener(action);
                break;
            default:
                break;
        }

        base.RemoveActionBoolean(action);
    }

    public override void SetBoolean(bool value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.maskable:
                _target.maskable = value;
                break;
            case AttributeType.isMaskingGraphic:
                //_target.isMaskingGraphic = value;
                break;
            case AttributeType.raycastTarget:
                _target.raycastTarget = value;
                break;
            case AttributeType.enabled:
                _target.enabled = value;
                break;
            default:
                break;
        }

        base.SetBoolean(value);
    }

    public override void SetColor(Color value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.color:
                _target.color = value;
                break;
            default:
                break;
        }

        base.SetColor(value);
    }

    public override void RemoveAllAction()
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveAllListeners();
                break;
            default:
                break;
        }
    }

}